import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/dialog/xindong_base_alert.dart';

class XinDongAlertMessage extends StatelessWidget {
  final String title;
  final String content;
  final TextAlign contentTextAlign;
  final String button;
  final bool showCancelButton;
  final bool hideWhenTouchOutside; // 触摸外部、返回键时，是否隐藏Dialog，默认true
  final Function()? onTapButton;

  const XinDongAlertMessage(
      {Key? key,
      this.title = "温馨提示",
      required this.content,
      this.contentTextAlign = TextAlign.left,
      this.button = "确定",
      this.showCancelButton = true,
      this.hideWhenTouchOutside = true,
      this.onTapButton})
      : super(key: key);

  XinDongAlertMessage.show(
      {super.key,
      this.title = "温馨提示",
      required this.content,
      this.contentTextAlign = TextAlign.left,
      this.button = "确定",
      this.showCancelButton = true,
      this.hideWhenTouchOutside = true,
      this.onTapButton}) {
    showAlertDialog(alert: this);
  }

  static Future<bool> delayShow({
    required String title,
    required String content,
    TextAlign? contentTextAlign,
    required String button,
    bool isCancel = true,
    Function()? onTapButton,
  }) async {
    bool? result = await showAlertDialog(
      alert: (XinDongAlertMessage(
        title: title,
        content: content,
        contentTextAlign: TextAlign.left,
        button: button,
        showCancelButton: isCancel,
        onTapButton: onTapButton,
      )),
    );
    return result == true;
  }

  @override
  Widget build(BuildContext context) {
    return XinDongBaseDialog(
      hideWhenTouchOutside: hideWhenTouchOutside,
      body: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            margin: const EdgeInsets.only(left: 26, right: 26, bottom: 24),
            padding: const EdgeInsets.only(left: 20, right: 20, bottom: 24, top: 16),
            width: double.infinity,
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), color: DZColors.dialog),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(color: DZColors.titleColor, fontSize: 16, fontWeight: FontWeight.w500),
                ),
                const SizedBox(height: 10),
                Text(content, style: const TextStyle(color: DZColors.contentColor, fontSize: 14), textAlign: contentTextAlign),
                const SizedBox(height: 16),
                Container(
                  height: 40,
                  constraints: const BoxConstraints(minWidth: 120),
                  child: XinDongCommonButton(button, onTap: () {
                    Get.back(result: true);
                    if (onTapButton != null) {
                      onTapButton!();
                    }
                  }),
                )
              ],
            ),
          ),
          if (showCancelButton)
            Container(
              width: 34,
              height: 34,
              alignment: Alignment.center,
              decoration: BoxDecoration(color: DZColors.dialog, borderRadius: BorderRadius.circular(34)),
              child: IconButton(
                onPressed: () => Get.back(result: false),
                icon: const Icon(Icons.close),
                padding: EdgeInsets.zero,
              ),
            ),
        ],
      ),
    );
  }
}
